package service.impl;

import com.sun.xml.internal.messaging.saaj.soap.JpegDataContentHandler;
import mapper.BookMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.Book;
import pojo.Place;
import pojo.vo.BookVo;
import service.BookService;
import service.PlaceService;
import utils.GetSqlSession;

import javax.swing.*;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-12-05
 * Time: 14:52
 */
public class BookServiceImpl implements BookService {

    SqlSession sqlSession = GetSqlSession.getSqlSession();
    BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
    /**
     * 获取所有书籍
     * @return
     */
    @Override
    public List<BookVo> listBook() {
        return bookMapper.listBook();
    }

    /**
     * 添加书籍
     */
    @Override
    public boolean addBook(String bookName, String author, String press, Long allNumber, String storagePlace){
        //先判断要添加的书名是否已经存在
        List<BookVo> books = bookMapper.listBookByBookName(bookName);
        if(books.size() > 0 ) return false;

        PlaceService placeService = new PlaceServiceImpl();
        Place place = placeService.getPlaceByName(storagePlace);
        if(place == null){
            JOptionPane.showMessageDialog(null, "请填写完整信息", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        //将书的剩余数也赋为总数
        Long surplusNumber = allNumber;
        return bookMapper.addBook(bookName, author, press, surplusNumber,allNumber,place.getId());
    }

    /**
     * 根据bookId进行删除
     * @param bookId
     * @return
     */
    @Override
    public boolean deleteBook1(Long bookId) {
            return bookMapper.deleteBook1(bookId);
    }

    /**
     * 根据书名进行删除
     * @param
     * @return
     */
    @Override
    public boolean deleteBook2(String name) {
        return bookMapper.deleteBook2(name);
    }

    /**
     * 根据书名进行模糊查询
     * @param
     * @return
     */
    @Override
    public List<BookVo> listBookByBookName(String name) {
        name = "%" + name + "%";
        return bookMapper.listBookByBookName(name);
    }

    /**
     * 根据书名进行精确查询
     * @param
     * @return
     */
    @Override
    public Book getBookByName(String name) {
        return bookMapper.getBookByName(name);
    }

    /**
     * 根据书id进行精确查询
     * @param
     * @return
     */
    @Override
    public List<BookVo> listBookByBookId(Long id) {
        return bookMapper.listBookByBookId(id);
    }

    /**
     * 修改书籍
     * @param
     * @return
     */
    @Override
    public boolean modifyBook(Book newBook,Long rawBookId) {
        return bookMapper.modifyBook(newBook,rawBookId);
    }
}
